package cn.wyl;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class EsTest {
    /**
     * 添加
     */
    @Test
    public void testAdd(){
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();
        //创建索引
        IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "1");
        //创建数据库映射的map
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",2);
        map.put("name","haha");
        map.put("age",108);
        map.put("sex",0);
        IndexResponse indexResponse = builder.setSource(map).get();
        //获取结果
        System.out.println(indexResponse);
        //关闭客户端
        client.close();
        }

    /**
     * 查询 获取文档
     */
    @Test
    public void testGet(){
        TransportClient client = ESClientUtil.getClient();
        // 指定索引/类型/文档id返回一个请求获取对象
        GetRequestBuilder builder = client.prepareGet("pethome", "pet", "1");
        // 响应获取对象
        GetResponse response = builder.get();
        //响应
        System.out.println(response);
        //响应源
        System.out.println(response.getSource());
        //响应源以map格式输出
        System.out.println(response.getSourceAsMap());
    }

    /**
     * 局部修改
     */
    @Test
    public void testUpdate(){
        TransportClient client = ESClientUtil.getClient();
        //修改索引对象
        UpdateRequestBuilder builder = client.prepareUpdate("pethome", "pet", "1");
        //创建数据库映射的map
        HashMap<String, Object> map = new HashMap<>();
        //局部修改
        map.put("name","haha1");
        //设置修改内容
        builder.setDoc(map).get();
        client.close();
    }
    /**
     * 删除
     */
    @Test
    public void testDelete(){
        TransportClient client = ESClientUtil.getClient();
        //指定文档id准备删除
        DeleteRequestBuilder builder = client.prepareDelete("pethome", "pet", "1");
        //执行删除
        builder.get();
        client.close();
    }

    /**
     * 构建综合查询
     * 宠物名字 大锤  age1-12 查询第二页 每页2条 按年龄倒序
     */
    @Test
    public void testFor(){
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();
        //批量添加假数据
        for (int i=1;i<100;i++){
            IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", ""+i);
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",i);
            map.put("name","王锤"+i);
            map.put("age",i);
            IndexResponse indexResponse = builder.setSource(map).get();
            System.out.println(indexResponse);
        }


    }

    /**
     * 构建综合查询
     * 宠物名字 大锤  age1-12 查询第二页 每页5条 按年龄倒序
     */
    @Test
    public void testSearch(){
        TransportClient client = ESClientUtil.getClient();

        SearchRequestBuilder builder = client.prepareSearch("pethome");
        //设置查询类型
        builder.setTypes("pet");
        //----------分页----------
        builder.setFrom(5).setSize(5);
        //排序
        builder.addSort("age", SortOrder.DESC);
        //-----------构建组合查询条件------------
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //设置组合查询条件中的条件
        boolQuery.must(QueryBuilders.matchQuery("name","s"));

        boolQuery.filter(QueryBuilders.rangeQuery("age").gte(1).lte(80))
                 .filter(QueryBuilders.termQuery("sex",1));

        //设置查询条件为组合查询
        builder.setQuery(boolQuery);

        //执行组合查询，获取条件查询的结果
        SearchResponse response = builder.get();

        //总命中结果
        SearchHits hits = response.getHits();
        System.out.println("总命中数："+hits.getTotalHits());
        //当前页的数据
        SearchHit[] list = hits.getHits();
        for (SearchHit row : list) {
            System.out.println(row.getSourceAsMap());
            
        }

    }






}
